package com.houtai.controller.black;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdBlack;
import com.houtai.bean.dto.balck.TdBlackListDto;
import com.houtai.service.black.BlackService;
import com.houtai.service.funcion.FuncionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by lizhun on 2017/7/19.
 */
@Controller
@RequestMapping(value = {"v5"})
public class BlackController {
    @Resource
    private BlackService blackService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private FuncionService funcionService;
 
    /**
     * @Purpose  分页加载黑名单
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/black/list")
    @Authority(AuthorityType.Login)
    public ModelAndView list(HttpServletRequest request, TdBlackListDto page){
        ModelAndView mv = new ModelAndView("black/list");
        mv.addObject("retInfo", blackService.findTdBlackList(page, request));
        mv.addObject("token", request.getParameter("token"));
        return mv;
    }

    /**
     * @Purpose  跳转到添加黑名单页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/black/to/add")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("black/add");
        mv.addObject("token", request.getParameter("token"));

        mv.addObject("black_type", request.getParameter("black_type"));
        mv.addObject("black_object_id", request.getParameter("black_object_id"));
        return mv;
    }
 
   
    /**
     * @Purpose   添加黑名单
     * @param     TdBlack
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/black/add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo insert(@Valid TdBlack TdBlack, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = blackService.insertTdBlack(TdBlack, request);
        }
        return retInfo;
    }



    /**
     * @Purpose   更新黑名单
     * @param     TdBlack
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/black/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(@Valid TdBlack TdBlack, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = blackService.updateTdBlack(TdBlack, request);
        }
        return retInfo;
    }

}
